package com.o.mj.service;

import com.o.mj.dao.LoginMapper;
import com.o.mj.pojo.Login;
import com.o.mj.pojo.Register;
import com.o.mj.pojo.UserInfo;
import com.o.mj.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class LoginService {
    @Autowired
    LoginMapper loginMapper;

    /**
     * 验证登录
     */
    public String justLogin(Login login) {
        return loginMapper.justLogin(login);
    }

    /**
     * 修改密码
     */
    public void changePwd(Login login) {
         loginMapper.changePwd(login);
    }

    /**
     * 获取密码
     */
    public String getPassword(Login login) {
        return loginMapper.getPassword(login);
    }

    /**
     * 验证是否为忘记密码
     */
    public String forget(Register register) {
        return loginMapper.forget(register);
    }

    /**
     * 更新密码
     */
    public String updatePassword(Register register) {
        return loginMapper.updatePassword(register);
    }

    /**
     * 验证登录身份
     */
    public String whoLogin(Login login) {
        return loginMapper.whoLogin(login);
    }

    /**
     * 注册用户
     */
    public boolean register(Register register) {
        //判断如果用户名 密码 角色昵称为空则返回false
        if (register.getPassword() == null || register.getPassword().equals("") || register.getAiName() == null || register.getAiName().equals("")
                || register.getUsername() == null || register.getUsername().equals("")) {
            return false;
        }
        if ((loginMapper.getUserName(register.getUsername())) != null) {
            return false;
        }
        /**
         * 进行新用户信息写入
         * 把密码进行加密放入类中
         * 保存登陆信息
         */
        register.setPassword(Md5Util.StringInMd5(register.getPassword()));
        Login login = new Login();
        String userId = UUID.randomUUID().toString();
        login.setUserId(userId);
        String id = UUID.randomUUID().toString();
        login.setId(id);
        login.setUserIdentity("0");
        login.setPassword(register.getPassword());
        login.setUsername(register.getUsername());
        boolean a = loginMapper.saveRegister(login);
        //如果保存异常 返回注册失败
        if (!a) {
            return false;
        }
        /**
         * 保存用户信息
         */
        UserInfo userinfo = new UserInfo();
        String UUd = UUID.randomUUID().toString();
        userinfo.setId(UUd);
        userinfo.setUserIdentity("0");
        userinfo.setAiName(register.getAiName());
        userinfo.setUserId(userId);
        userinfo.setIfWork("1");
        return loginMapper.saveUserInfo(userinfo);

    }
}
